<template>
	<view class="login-box u-flex u-flex-center u-flex-column u-flex-items-center">
		<view class="f-s-24 u-m-b-80 f-w-b">立即登录</view>
		<!-- <image src="@/static/logo.png" mode="aspectFit" style="width: 160rpx;height: 160rpx;border-radius: 50%;background-color: #fff;"></image> -->
		<view class="form-box">
			<u--form :model="loginForm" ref="uForm" errorType="toast" labelWidth="80px"
				:labelStyle="{marginBottom:'12px',fontWeight:'bold'}">
				<u-form-item prop="account" label="手机号:" labelPosition="top">
					<up-input placeholder="请输入手机号码" border="none" type="number" :maxlength="11"
						v-model="loginForm.account" :customStyle="{
		                        padding: '20rpx 40rpx',
		                        borderRadius: '110rpx',
		                        fontSize: '32rpx',
								background: '#F6FBFF',
								color: '#999999'
		                    }"></up-input>
				</u-form-item>
				<u-form-item prop="pwd" label="密码:" labelPosition="top">
					<u-input type="password" v-model="loginForm.pwd" placeholder="请输入密码" border="none" :customStyle="{
									padding: '20rpx 40rpx',
									borderRadius: '110rpx',
		                            fontSize: '32rpx',
									background: '#F6FBFF',
									color: '#999999'
		                        }"></u-input>
				</u-form-item>
			</u--form>
			<view class="u-m-t-60">
				<u-button color="#2080F7" text="立即登录" :customStyle="{
					width: '100%',
					height: '96rpx',
					borderRadius: '180rpx',
					fontSize: '32rpx',
					fontWeight: 'bold'
				}" @click="login"></u-button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import store from '@/store';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		ref
	} from 'vue'
	import {
		toast
	} from '@/utils/common.js';
	
	const loginForm = ref({
		account: '',
		pwd: ''
	})

	onLoad(() => {
		uni.login({
			provider: 'weixin',
			success(resp) {
				loginForm.value.loginCode = resp.code;
			}
		})
	})

	/**
	 * 登录
	 */
	const login = () => {
		store.dispatch('Login', loginForm.value).then(() => {
			loginSuccess()
		})
	}

	// 登录成功后，处理函数
	function loginSuccess() {
		const prev = getCurrentPages()[0].route
		// 设置用户信息
		store.dispatch('GetInfo').then(res => {
			toast("登录成功")
			if (prev == 'pages/index') {
				uni.switchTab({
					url: '/pages/index'
				})
				return
			}
			uni.navigateBack()
		})
	}
</script>

<style lang="scss" scoped>
	.login-box {
		margin-top: 180px;
	}
</style>